package com.example.hzq.food.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.bean.MyBean;

import java.util.List;

/**
 * Created by hzq on 2017/6/14.
 */

public class Fragment4adapter extends RecyclerView.Adapter {
    private List<MyBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    public Fragment4adapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<MyBean> list){
        this.list=list;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_itemname,tv_itemintro;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView3);
            tv_itemname = (TextView) itemView.findViewById(R.id.textView25);
            tv_itemintro = (TextView) itemView.findViewById(R.id.textView26);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.from(parent.getContext()).inflate(R.layout.item1,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyBean myBean = list.get(position);
        if (myBean== null)
            return;

        ViewHolder viewholder = (ViewHolder) holder;
        viewholder.imageView.setImageResource(myBean.getImgid());
        viewholder.tv_itemname.setText(myBean.getItemname());
        viewholder.tv_itemintro.setText(myBean.getItemntro());
        //条目点击监听事件
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前条目id
                String strid=String.valueOf(list.get(position).getItemname());
                Toast.makeText(context, strid, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
